//
//  SVPlayButtonView.m
//  SilenceVoice
//
//  Created by 张诗宇 on 16/1/27.
//  Copyright © 2016年 张诗宇. All rights reserved.
//

#import "SVPlayButtonView.h"
#import "SVMPViewController.h"

@implementation SVPlayButtonView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

+ (instancetype)shareButton {
    
    static SVPlayButtonView *playButton = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        playButton = [[SVPlayButtonView alloc] init];
        [playButton createSubviews];
        
    });
    return playButton;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
    
}

- (void)createSubviews {
    
    self.backgroundColor = [UIColor clearColor];
    self.frame = CGRectMake(375 * 0.4 * Screen_Width_Scale, -24, 375 * 0.2 * Screen_Width_Scale, 667 * 0.2 * Screen_Height_Scale);
    self.userInteractionEnabled = YES;
    
    // 背景
    self.buttonBackground = [[UIImageView alloc] initWithFrame:self.bounds];
    
    // 设置圆角
    self.buttonBackground.layer.masksToBounds = YES;
    self.buttonBackground.layer.cornerRadius = self.frame.size.width / 2;
    
    [self addSubview:_buttonBackground];
    self.buttonBackground.tag = 1000;
    
    // 条
    self.line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375 * 0.2 * Screen_Width_Scale, 375 * 0.2 * Screen_Height_Scale)];
    
    _line.image = [UIImage imageNamed:@"tabbar_np_loop@2x.png"];
    _line.center = CGPointMake(375 * 0.1 * Screen_Width_Scale, 375 * 0.1 * Screen_Width_Scale);
    _line.userInteractionEnabled = YES;
    [self.buttonBackground addSubview:_line];
    
    
    // 旋转imageView
    self.musicButton = [SVBaseButton buttonWithType:UIButtonTypeCustom];
    
    self.musicButton.frame = CGRectMake(0, 0, 375 * 0.15 * Screen_Width_Scale, 375 * 0.15 * Screen_Width_Scale);
    
    self.musicButton.backgroundColor = [UIColor clearColor];
    
    self.musicButton.layer.masksToBounds = YES;
    
    self.musicButton.layer.cornerRadius = 375 * 0.075 * Screen_Width_Scale;
    
    self.musicButton.center = CGPointMake(375 * 0.1 * Screen_Width_Scale, 375 * 0.1 * Screen_Width_Scale);
    
    [_line addSubview:_musicButton];
    
    
    // 播放按钮
    self.playButton = [SVBaseButton buttonWithType:UIButtonTypeCustom];
    _playButton.backgroundColor = [UIColor clearColor];
    _playButton.frame = CGRectMake(0, 0, 375 * 0.15 * Screen_Width_Scale, 375 * 0.15 * Screen_Width_Scale);
    _playButton.layer.masksToBounds = YES;
    _playButton.layer.cornerRadius = 375 * 0.075 * Screen_Width_Scale;
    _playButton.center = CGPointMake(375 * 0.1 * Screen_Width_Scale, 375 * 0.1 * Screen_Width_Scale);
    
    [self.playButton setImage:[UIImage imageNamed:@"tabbar_np_play@2x.png"] forState:UIControlStateNormal];
    _playButton.alpha = 0.6;
    [self.playButton addTarget:self action:@selector(playMusic:) forControlEvents:UIControlEventTouchUpInside];
    
    self.playButton.userInteractionEnabled = YES;
    
    [_line addSubview:_playButton];
    
}

-(void)playMusic:(UIButton *)btn
{

    SVMPViewController *musicPlayer = [SVMPViewController sharePlayerViewController];
    
    
    [self.window.rootViewController presentViewController:musicPlayer animated:YES completion:^{
        
        [self startCircling];
        
    }];
}

- (void)startCircling {
    
#pragma mark - musicBtn旋转动画
    //改变view Z轴
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //旋转的起始值 从零开始转
    animation.fromValue = [NSNumber numberWithInt:0];
    
    //最终旋转的角度是360°
    animation.toValue = [NSNumber numberWithInt:M_PI * 2];
    
    //旋转时长
    animation.duration = 5;
    
    //重复次数 0 为无限次
    animation.repeatCount = NSIntegerMax;
    
    //旋转结束后是否要逆向返回原位置
    animation.autoreverses = NO;
    
    //是否按照结束位置继续旋转
    animation.cumulative = YES;
    
    //给view的layer层添加动画,key是标记  随便起名字
    [_musicButton.layer addAnimation:animation forKey:@"circle"];
    //    NSLog(@"%f", self.musicBtn.layer.speed) ;
    _musicButton.layer.speed = 1.0;
    //    NSLog(@"%f", self.musicBtn.layer.speed) ;
    
    _playButton.alpha = 0;
    _playButton.enabled = NO;
  
}

- (void)stopCircling {
    
    _musicButton.layer.speed = 0;
    
    _playButton.alpha = 0;
    
    _playButton.enabled = YES;
    
}


@end
